
<?php	
	
	require_once('../../lib/'.'BaseDir.inc.htm');		
	require_once (EIU_LIB.'View.class.htm');
	require_once(EIU_LIB.'DBCommon.class.htm');
	require_once (EIU_CLASS.'LoginClass.htm');
	
	@session_start(); 	
	
	$result = "";
	
	$oDB = new DBCommon();
	$conn =  $oDB->stl_openConn();
	
	$fromDate = $_POST["FromDate"];
	$toDate = $_POST["ToDate"];
	
	if($fromDate == "") $fromDate = "1970-01-01";
	if($toDate == "") $toDate = date("Y-m-d");
	
	
	/*
	$q = "
	
		SELECT SumSubTotalVND,SumTotalVND,SumCommissionAmount,ATB.CusID,ATB.ComName AS CusName,ATB.ComAddress AS CusAddress,ATB.ParentID,cus_list_tbl.ComName AS ParentName,cus_list_tbl.ComAddress AS ParentAddress,cus_list_tbl.ComTel AS ParentTel
		FROM ( 
			SELECT SUM(`SubTotalVND`) AS SumSubTotalVND,SUM(`TotalVND`) AS SumTotalVND,`ComName`,`ComTel`,`ComAddress`,`CusID`,`ParentID`, SUM(`CommissionAmount`) AS SumCommissionAmount 
			
			FROM `io_tbl` LEFT JOIN cus_list_tbl ON `ReceiptPersonID` = `CusID` 
			
			WHERE `io_tbl`.`isDeleted` = 0 AND cus_list_tbl.`isDeleted` = 0 AND `ComName` LIKE '%%' AND `DeliveryReceiptDate` >= '".$fromDate."' AND `DeliveryReceiptDate` <= '".$toDate."' AND ParentID != 0
			
			GROUP BY ReceiptPersonID ) AS ATB LEFT JOIN cus_list_tbl ON ATB.ParentID = cus_list_tbl.CusID 

		WHERE 1
	";
	*/
	$q = "
	
		SELECT 
			SUM(SumSubTotalVND) AS SumSubTotalVND, SUM(SumTotalVND) AS SumTotalVND,
			SUM(SumCommissionAmount) AS SumCommissionAmount,
			ATB.CusID,ATB.ComName AS CusName,
			ATB.ComAddress AS CusAddress,
			ATB.ParentID,
			cus_list_tbl.ComName AS ParentName,
			cus_list_tbl.ComAddress AS ParentAddress,
			cus_list_tbl.ComTel AS ParentTel 
		FROM 
			( 
				SELECT 
					SUM(`SubTotalVND`) AS SumSubTotalVND,SUM(`TotalVND`) AS SumTotalVND,
					`ComName`,`ComTel`,`ComAddress`,`CusID`,`ParentID`, 
					SUM(`CommissionAmount`) AS SumCommissionAmount 
				FROM `io_tbl` LEFT JOIN cus_list_tbl ON `ReceiptPersonID` = `CusID` 
				WHERE `io_tbl`.`isDeleted` = 0 AND cus_list_tbl.`isDeleted` = 0 AND `ComName` LIKE '%%' AND `DeliveryReceiptDate` >= '".$fromDate."' AND `DeliveryReceiptDate` <= '".$toDate."' AND ParentID != 0 
				GROUP BY ReceiptPersonID 
			) AS ATB LEFT JOIN cus_list_tbl ON ATB.ParentID = cus_list_tbl.CusID 
		WHERE 1 
		GROUP BY ParentID
	";
	
	//echo $q;
		
	$rs = $oDB->stl_db_select($q, $conn);
	
	$oDB->stl_closeConn($conn);
		
	echo json_encode($rs);
	
?>